var header_h, UIActionProgress, ajpush, ns
var app = new Vue({
    el: '#app',
    data: {
        device_id: '',
        device_type: '',
        count: 0
    },
    methods: {
        openFrame: function () {
            var that = this;
            that.count++;
            api.getPrefs({
                key: 'uid'
            }, function (ret, err) {
                if (!ret.value) {
                    setTimeout(function () {
                        var frames = api.frames();
                        for (var i = 0; i < frames.length; i++) {
                            if (frames[i].name != 'login_win' && frames[i].name != 'login_frm') {
                                api.closeFrame({
                                    name: frames[i].name
                                });
                            }
                        }
                    }, 500)
                    ns.openFrame('login_win', null, {
                        ismain: true,
                        rect: {
                            x: 0,
                            y: 0,
                            w: 'auto',
                            h: 'auto'
                        }
                    });
                } else {
                    setTimeout(function () {
                        api.closeFrame({
                            name: 'login_win'
                        });
                        api.closeFrame({
                            name: 'login_frm'
                        });
                    }, 500)
                    ns.openFrame('index_win', null, {
                        ismain: true,
                        rect: {
                            x: 0,
                            y: 0,
                            w: 'auto',
                            h: 'auto'
                        }
                    });
                }
            });
        },
        //保存极光用户注册id
        save_jg_cid: function() {
            var that = this;
            ajpush.getRegistrationId(function(ret, err) {
                if (ret && ret.id) {
                    that.device_id = ret.id;
                    api.setPrefs({
                        key: 'jpush_device_id',
                        value: ret.id
                    });
                }
            });
        },
        //初始化
        jp_init: function () {
            var that = this;
            if (that.device_type == 'android') {
                ajpush.init(function (ret, err) { });
            }
            setTimeout(function () {
                ajpush.setAuth({
                    auth: true
                }, function (ret) {
                    console.log(JSON.stringify(ret))
                });
            }, 500);
            setTimeout(function () {
                // that.jp_setListener();
                that.jp_Add_Listener();
                that.jp_Resume_Push();
                that.save_jg_cid();
                ajpush.setBadge({
                    badge: 0
                });
            }, 2000);
        },
        //APP界面打开状态获取消息
        jp_setListener: function () {
            var that = this;
            ajpush.setListener(function (ret, err) {
                if (ret && ((ret.title && that.device_type == 'ios') || that.device_type == 'android')) {
                    var rep = ret.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function (res, err) {
                        if (res.value) {
                            ajpush.setBadge({
                                badge: 0
                            });
                            that.jp_process(rep, true);
                        } else {
                            api.sendEvent({
                                name: 'onlogout'
                            });
                        }
                    });
                }
            });
        },
        //APP界面隐藏状态获取消息
        jp_Add_Listener: function () {
            var that = this;
            api.addEventListener({
                name: 'appintent'
            }, function (ret, err) {
                if (ret && ret.appParam.ajpush) {
                    var rep = ret.appParam.ajpush.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function (res, err) {
                        if (res.value) {
                            that.jp_process(rep, false);
                        } else {
                            api.sendEvent({
                                name: 'onlogout'
                            });
                        }
                    });
                }
            });
            api.addEventListener({
                name: 'noticeclicked'
            }, function (ret, err) {
                if (ret && ret.value) {
                    var rep = ret.value.extra;
                    api.getPrefs({
                        key: 'uid'
                    }, function (res, err) {
                        if (res.value) {
                            ajpush.setBadge({
                                badge: 0
                            })
                            that.jp_process(rep, false);
                        } else {
                            api.sendEvent({
                                name: 'onlogout'
                            });
                        }
                    });
                }
            });
            api.addEventListener({
                name: 'pause'
            }, function (ret, err) {
                ajpush.onPause(); //监听应用进入后台
                that.jp_Resume_Push();
            });
            api.addEventListener({
                name: 'resume'
            }, function (ret, err) {
                ajpush.onResume(); //监听应用恢复到前台
                that.jp_Resume_Push();
            });
        },
        //断开重连
        jp_Resume_Push: function () {
            var that = this;
            if (that.jg_timeout) {
                clearTimeout(that.jg_timeout);
                that.jg_timeout = null;
            }
            setTimeout(function () {
                that.jp_resumePush();
            }, 1000);
        },
        //断开重连
        jp_resumePush: function () {
            var that = this;
            ajpush.isPushStopped(function (ret, err) {
                if (!ret || ret.isStopped != 0) {
                    ajpush.resumePush(function (ret, err) { });
                }
                that.jg_timeout = setTimeout(function () {
                    that.jp_resumePush();
                }, 10 * 1000);
            });
        },
        jp_openwin: function (name, title, params) {
            var that = this;
            api.closeToWin({
                name: 'root'
            });
            ns.openWin(name, title, params, {
                ismain: true
            });
        },
        jp_process: function (rep, pop_required) {
            var that = this;
            if (rep.type == 'dofouselogout') {
                ns.removeUid(false);
                api.alert({
                    title: '提示',
                    msg: rep.msg,
                }, function (ret, err) {
                    api.sendEvent({
                        name: 'onlogout'
                    });
                });
                return;
            }
            api.sendEvent({
                name: 'ajpushnotifyalert',
                extra: {
                    id: rep.id,
                    type: rep.type,
                    msgType: rep.msgType
                }
            });
            api.sendEvent({
                name: 'reloadunreadmsgcount'
            });
            return;
            if (rep.sound && pop_required) {
                api.startPlay({
                    path: rep.sound
                }, function (ret, err) { });
            }
        },
        get_all: function () {
            var that = this;
            // that.upload_log();
            app.openFrame();
            setTimeout(function () {
                app.jp_init();
            }, 1000);
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.confirmPer('storage', function () { })
    UIActionProgress = api.require('UIActionProgress');
    app.device_type = api.systemType;
    ajpush = api.require('ajpush');
    app.get_all();
    api.addEventListener({
        name: 'onlogin'
    }, function () {
        api.closeToWin({
            name: 'root'
        });
        setTimeout(function () {
            app.openFrame();
        }, 500)
    });
    api.addEventListener({
        name: 'onlogout'
    }, function () {
        api.closeToWin({
            name: 'root'
        });
        app.openFrame();
    });
    var last_out_time;
    api.addEventListener({
        name: 'keyback'
    }, function (ret, err) {
        if (last_out_time && last_out_time.valueOf() >= (new Date().valueOf() - 1000)) {
            api.closeWidget({
                silent: true
            });
            return;
        }
        last_out_time = new Date();
        api.toast({
            msg: '再按一次退出程序',
            duration: 1000
        });
    });
};
